﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Diagnostics;
using System.Net;
using XmHttpServer;
using System.Reflection;

namespace XmHttpServerForm
{
    public partial class MainForm2 : Form
    {
        public MainForm2()
        {
            InitializeComponent();
        }
        private HttpServer server;
        private void button1_Click(object sender, EventArgs e)
        {
            Assembly ab = Assembly.GetExecutingAssembly();//获取当前运行的程序集
            server = new HttpServer();
            server.Assembly = ab;
            server.addDirectory("/",Application.StartupPath);
            server.RequestReceived += new HttpRequestEventHandler(server_RequestReceived);
            IPEndPoint endPoint = new IPEndPoint(IPAddress.Parse("127.0.0.1"), 9999);
            server.EndPoint = endPoint;
            server.Start();
            Process.Start(String.Format("http://{0}/", server.EndPoint));
        }

        void server_RequestReceived(object sender, HttpRequestEventArgs e)
        {
            
            //using (var writer = new StreamWriter(e.Response.OutputStream))
            //{
            //    writer.Write("Hello world!");
            //}
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (server != null)
            {
                if (server.State != HttpServerState.Stopped && server.State != HttpServerState.Stopping)
                {
                    server.Stop();
                }
            }
        }





    }
}
